package com.system.fkjslee.hos;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

public class LogActivity extends Activity implements View.OnClickListener{

    public static String url = "http://118.24.11.68:8080";
//    public static String url = "http://172.24.8.233:8080";
    public static String account = "";

    private EditText etID;
    private Button btnLog;
    private Button btnQuit;
    private long mExitTime = -2005;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);
        initView();
    }

    private void initView() {
        etID = findViewById(R.id.etID);
        btnLog = findViewById(R.id.btnLog);
        btnQuit = findViewById(R.id.btnQuit);

        btnLog.setOnClickListener(this);
        btnQuit.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnLog:
               toAppointOrQueryActivity();
               break;
            case R.id.btnQuit:
                finish();
                break;
        }
    }

    private void toAppointOrQueryActivity() {
        account = etID.getText().toString();
        String param = "account=" + account + "&pwd=" + "";
        HttpThread httpThread = new HttpThread(url + "/login", param);
        new Thread(httpThread).start();
        String result = httpThread.getResult();
        JSONObject jsonObject;
        String type = null;
        try {
            jsonObject = new JSONObject(result);
            type = jsonObject.getString("type");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Toast.makeText(this, "login返回 : " + result, Toast.LENGTH_LONG).show();
        if("p".equals(type)) {
            Intent intent = new Intent(this, AppointOrQuery.class);
            account = etID.getText().toString();
            startActivity(intent);
        } else {
            Toast.makeText(this, "错误的账户, 请重试", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 两次点击返回退出程序
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
